package com.mzpai.product.service.impl;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mzpai.product.constant.OrderConstants;
import com.mzpai.product.mapper.UserMapper;
import com.mzpai.product.model.RechargeRecord;
import com.mzpai.product.model.SystemStoreDdfBill;
import com.mzpai.product.model.SystemStoreDdfTrade;
import com.mzpai.product.model.User;
import com.mzpai.product.service.RechargeRecordService;
import com.mzpai.product.service.StoreDdfBillService;
import com.mzpai.product.service.SystemStoreDdfTradeService;
import com.mzpai.product.service.UserService;
import com.mzpai.product.vo.DeductGoldRechargeVo;
import com.mzpai.product.vo.UserInfoVo;
import com.mzpai.service.impl.BaseServiceImpl;
import com.mzpai.util.ArithUtil;
import com.mzpai.util.DateUtil;
import com.mzpai.util.PageUtil;
import com.mzpai.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

	Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Autowired
	private StoreDdfBillService storeDdfBillService;
	
	@Autowired
	private SystemStoreDdfTradeService systemStoreDdfTradeService;
	
	@Autowired
	private RechargeRecordService rechargeRecordService;

	@Autowired 
	private UserMapper userMapper;

	@Override
	public User queryByUserId(Integer userId) {
		User user=userMapper.queryByUserId(userId);
		return user;
	}

	@Override
	public User queryByUserText(String userText) {
		User user=userMapper.queryByUserText(userText);
		return user;
	}

	
	@Override
	public void addDeductGoldRecharge(DeductGoldRechargeVo deductGoldRechargeVo,Integer userId) {
		Integer storeId=deductGoldRechargeVo.getStoreId();
		BigDecimal totalMoney = deductGoldRechargeVo.getTotalMoney();
		
		Date currentDate=new Date();
		Integer currentDateTime=(int) (currentDate.getTime()/1000);
		String businessId=OrderConstants.DEDUCTGOLDORDER_BEGIN+DateUtil.getDateFormateDate()+StringUtils.randomString(6,true);
		
		
		//根据id更新对应已使用额度和剩余总额
		SystemStoreDdfBill addStoreDdfBill=new SystemStoreDdfBill();
		Date effectTimeStart=DateUtil.dateFormate(currentDate,DateUtil.DATE_HH_MM_SS);
		//添加永久有效   往后延100年
		Date effectTimeEnd=DateUtil.addDays(effectTimeStart, 36500);
		addStoreDdfBill.setGoodsType(4);
		addStoreDdfBill.setUid(userId);
		addStoreDdfBill.setStoreId(storeId);
		addStoreDdfBill.setOrderId(businessId);
		addStoreDdfBill.setTotalAmount(totalMoney);
		addStoreDdfBill.setBalance(totalMoney);
		BigDecimal usedAmount=new BigDecimal(0);
		addStoreDdfBill.setUsedAmount(usedAmount);
		addStoreDdfBill.setAddTime(currentDateTime);
		addStoreDdfBill.setEffectTimeStart(effectTimeStart);
		addStoreDdfBill.setEffectTimeEnd(effectTimeEnd);
		addStoreDdfBill.setSendTime(effectTimeStart);
		addStoreDdfBill.setStatus((byte)1);
		addStoreDdfBill.setDdfAwardId(0);
		addStoreDdfBill.setDdfConfigId(0);
		addStoreDdfBill.setModifyTime(effectTimeStart);
		storeDdfBillService.save(addStoreDdfBill);
		
		//记录单单反抵扣金交易明细(eb_system_store_ddf_trade)
		SystemStoreDdfTrade systemStoreDdfTrade=new SystemStoreDdfTrade();
		systemStoreDdfTrade.setStoreId(storeId);
		systemStoreDdfTrade.setUid(userId);
		systemStoreDdfTrade.setBillId(0l);
		systemStoreDdfTrade.setOrderId(businessId);
		systemStoreDdfTrade.setType((byte)1);
		systemStoreDdfTrade.setNumber(totalMoney);
		systemStoreDdfTrade.setAddTime(currentDateTime);
		systemStoreDdfTradeService.save(systemStoreDdfTrade);
		
		//记录充值历史
		RechargeRecord rechargeRecord=new RechargeRecord();
		rechargeRecord.setAddTime(effectTimeStart);
		rechargeRecord.setBusinessId(businessId);
		rechargeRecord.setStoreId(storeId);
		rechargeRecord.setTotalMoney(totalMoney);
		rechargeRecord.setUserId(userId);
		rechargeRecordService.save(rechargeRecord);
		
	}
	
	@Override
	public PageInfo<UserInfoVo> getUserListByPage(Map<String, Object> map, Integer pageNum,Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<UserInfoVo> listUserInfoVo=userMapper.getUserListByPage(map);
		return PageUtil.toPageInfo(listUserInfoVo);
	}

	@Override
	public BigDecimal getTotalDeducteGold(Integer storeId, Integer userId) {
		BigDecimal balanceTotal=null;
		//查询用户在门店下可以抵扣金总额
		Map<String,Object> ddfBillMap=new HashMap<String,Object>();
		ddfBillMap.put("storeId", storeId);
		ddfBillMap.put("userId", userId);
		List<SystemStoreDdfBill> storeDdfBillList=storeDdfBillService.getEffectiveStoreDdfBillList(ddfBillMap);
		if(storeDdfBillList!=null && !storeDdfBillList.isEmpty()){
			double blaceTotalValue=0.00d;
			for(SystemStoreDdfBill storeDdfBill:storeDdfBillList){
				//发放总金额
				BigDecimal totalAmount=storeDdfBill.getTotalAmount();
				//已使用总金额
				BigDecimal usedAmount=storeDdfBill.getUsedAmount();
				//余额
				BigDecimal balance=storeDdfBill.getBalance();
				//防止 使用总额大于发放总额
				if(totalAmount.doubleValue()>usedAmount.doubleValue()){
					Double balanceValue=balance.doubleValue();
					blaceTotalValue=ArithUtil.add(blaceTotalValue,balanceValue);
				}
			}
			balanceTotal=new BigDecimal(blaceTotalValue);
		}else{
			balanceTotal=new BigDecimal(0);
		}
		return balanceTotal;
	}

	
}